package com.rdc.qingkou.presenter;

import android.text.TextUtils;

import com.rdc.qingkou.base.BasePresenterImpl;
import com.rdc.qingkou.contract.RegisterContract;
import com.rdc.qingkou.model.RegisterModel;
import com.rdc.qingkou.model.imodel.ICheckRegisteCallBack;
import com.rdc.qingkou.model.imodel.IRegisteCallBack;

/**
 * Created by asus on 2017/9/14.
 */

public class RegisterPresenter extends BasePresenterImpl<RegisterContract.View> implements RegisterContract.PresenterI {

    private RegisterModel mRegisterModel = new RegisterModel();
    private String mRegisteResult = "注册成功！";
    private  String mChangPasswordResult = "修改成功，请直接登录！";


    @Override
    public boolean checkisCorrectInput() {
        if (mView.getAccount().matches("((13\\d)|(15\\d)|(18\\d))\\d{8}")){
              if (TextUtils.isEmpty(mView.getPassword())){
                  mView.showPasswordEmptyInfo();
                  return false;
              }else if (!TextUtils.isEmpty(mView.getPassword())
                    &&!TextUtils.isEmpty(mView.getPasswordAgain())
                    &&mView.getPassword().equals(mView.getPasswordAgain())){
                return true;
            }else{
                mView.showPasswordInfo();
                return false;
            }
        }else {
            mView.showInputInfo();
            return false;

        }

}

    @Override
    public void checkIsRegisted() {
        if (mView.getAccount().matches("((13\\d)|(15\\d)|(18\\d))\\d{8}")){
            mRegisterModel.isRegisted(mView.getAccount(), new ICheckRegisteCallBack() {
                @Override
                public void success() {
                    mView.showRegisted();
                }

                @Override
                public void fail(String msg) {
                     mView.showRegisteFail(msg);
                }
            });

        }
    }

    @Override
    public void checkIsCorrectPhoneNumber() {
         if (!mView.getAccount().matches("((13\\d)|(15\\d)|(18\\d))\\d{8}")){
                    mView.showInputInfo();

         }
    }

    @Override
    public void setButtonClickable() {
        if (mView.getAccount().length() == 11 && mView.getCode().length() == 6) {
            mView.setButtonClickable(true);

        }

    else{
            mView.setButtonClickable(false);
        }
    }


    @Override
    public void getCode() {
        if (mView.getAccount().matches("((13\\d)|(15\\d)|(18\\d))\\d{8}")){
            mRegisterModel.getConfirmCode(mView.getAccount(), new IRegisteCallBack() {
                @Override
                public void success(String code) {
                    mView.showRegisteFail(code);
                }

                @Override
                public void fail(String reason) {

                }
            });
            mView.countdown();
        }else {
            mView.showInputInfo();
        }
    }


    @Override
    public void registe(boolean isChangePassword) {
        if (checkisCorrectInput()){
              if (isChangePassword==false){
                  mRegisterModel.registe(mView.getAccount(), mView.getPassword(), mView.getPasswordAgain(), mView.getCode(), new IRegisteCallBack() {
                      @Override
                      public void success(String code) {
                          mView.showJumpToLoginInfo(mRegisteResult);
                          mView.jumpToLogin();
                      }

                      @Override
                      public void fail(String reason) {
                          mView.showRegisteFail(reason);
                      }
                  });

              }else {
                  mRegisterModel.changePassword(mView.getAccount(), mView.getPassword(), mView.getPasswordAgain(), mView.getCode(), new IRegisteCallBack() {
                      @Override
                      public void success(String code) {
                          mView.showJumpToLoginInfo(mChangPasswordResult);
                          mView.jumpToLogin();
                      }

                      @Override
                      public void fail(String reason) {
                          mView.showRegisteFail(reason);
                      }
                  });

              }
        }

    }
}
